<template>
  <div class="container">
    <div class="container">
      <div class="now">
        <h4>
          正在进行 <span>{{ uncompleted.length }}</span>
        </h4>
        <ul class="active">
          <li v-for="item in uncompleted" :key="item.name">
            <input
              type="checkbox"
              :checked="item.done"
              @click="handelCompleted(item)"
            />
            {{ item.name }}
            <button @click="handelDel(item.name)">删除</button>
          </li>
        </ul>
      </div>
      <div class="done">
        <h4>
          已经完成 <span>{{ completed.length }}</span>
        </h4>
        <ul class="completed">
          <li v-for="item in completed" :key="item.name">
            <input
              type="checkbox"
              :checked="item.done"
              @click="handelCompleted(item)"
            />
            {{ item.name }}
            <button @click="handelDel(item.name)">删除</button>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'TodoItem',
  props: {
    todo: {
      type: Array,
      required: true,
    },
  },
  computed: {
    completed() {
      return this.todo.filter((item) => item.done)
    },
    uncompleted() {
      return this.todo.filter((item) => !item.done)
    },
  },
  methods: {
    handelCompleted(item) {
      item.done = !item.done
      localStorage.setItem('todo', JSON.stringify(this.todo))
    },
    handelDel(name) {
      const result = window.confirm('Are you sure you want to delete')
      if (result) {
        let index = this.todo.filter((item) => item.name === name)
        this.todo.splice(index, 1)
        localStorage.setItem('todo', JSON.stringify(this.todo))
      }
    },
  },
}
</script>
<style></style>
